{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 323,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "import warnings\n",
    "warnings.filterwarnings(\"ignore\")\n",
    "my = pd.read_csv('maoyanfilms_clean.csv')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 324,
   "metadata": {},
   "outputs": [],
   "source": [
    "my = my.drop(['Unnamed: 0','films_id','films_name','films_comment_num','films_director','films_actor','films_introduction','films_comment'],axis=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 325,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>films_type</th>\n",
       "      <th>films_area</th>\n",
       "      <th>films_duration</th>\n",
       "      <th>film_first_time</th>\n",
       "      <th>films_score</th>\n",
       "      <th>films_box_office</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>喜剧 , 爱情</td>\n",
       "      <td>中国大陆</td>\n",
       "      <td>122分钟</td>\n",
       "      <td>2010-12-22</td>\n",
       "      <td>7.4</td>\n",
       "      <td>47200.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>爱情</td>\n",
       "      <td>中国香港</td>\n",
       "      <td>117分钟</td>\n",
       "      <td>2009-01-26</td>\n",
       "      <td>6.1</td>\n",
       "      <td>11299.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>爱情 , 剧情</td>\n",
       "      <td>中国台湾,中国大陆</td>\n",
       "      <td>128分钟</td>\n",
       "      <td>2012-02-14</td>\n",
       "      <td>7.8</td>\n",
       "      <td>13700.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>爱情</td>\n",
       "      <td>中国大陆,中国香港</td>\n",
       "      <td>112分钟</td>\n",
       "      <td>2012-02-09</td>\n",
       "      <td>7.1</td>\n",
       "      <td>2137.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>爱情 , 音乐 , 奇幻</td>\n",
       "      <td>中国香港,中国台湾</td>\n",
       "      <td>101分钟</td>\n",
       "      <td>2007-07-31</td>\n",
       "      <td>8.5</td>\n",
       "      <td>33.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15425</th>\n",
       "      <td>NaN</td>\n",
       "      <td>中国香港</td>\n",
       "      <td>13分钟</td>\n",
       "      <td>2015-12</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15426</th>\n",
       "      <td>剧情 , 短片</td>\n",
       "      <td>中国大陆</td>\n",
       "      <td>20分钟</td>\n",
       "      <td>2019-06-25</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15427</th>\n",
       "      <td>纪录片 , 短片</td>\n",
       "      <td>中国大陆</td>\n",
       "      <td>4分钟</td>\n",
       "      <td>2016</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15428</th>\n",
       "      <td>喜剧</td>\n",
       "      <td>中国香港</td>\n",
       "      <td>125分钟</td>\n",
       "      <td>2012-10-11</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15429</th>\n",
       "      <td>纪录片</td>\n",
       "      <td>中国台湾</td>\n",
       "      <td>61分钟</td>\n",
       "      <td>2018-10-25</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>15430 rows × 6 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "           films_type films_area films_duration film_first_time  films_score  \\\n",
       "0            喜剧 , 爱情        中国大陆          122分钟      2010-12-22          7.4   \n",
       "1                 爱情        中国香港          117分钟      2009-01-26          6.1   \n",
       "2            爱情 , 剧情   中国台湾,中国大陆          128分钟      2012-02-14          7.8   \n",
       "3                 爱情   中国大陆,中国香港          112分钟      2012-02-09          7.1   \n",
       "4       爱情 , 音乐 , 奇幻   中国香港,中国台湾          101分钟      2007-07-31          8.5   \n",
       "...               ...        ...            ...             ...          ...   \n",
       "15425             NaN       中国香港           13分钟         2015-12          NaN   \n",
       "15426        剧情 , 短片        中国大陆           20分钟      2019-06-25          NaN   \n",
       "15427       纪录片 , 短片        中国大陆            4分钟            2016          NaN   \n",
       "15428             喜剧        中国香港          125分钟      2012-10-11          NaN   \n",
       "15429            纪录片        中国台湾           61分钟      2018-10-25          NaN   \n",
       "\n",
       "       films_box_office  \n",
       "0               47200.0  \n",
       "1               11299.0  \n",
       "2               13700.0  \n",
       "3                2137.0  \n",
       "4                  33.0  \n",
       "...                 ...  \n",
       "15425               NaN  \n",
       "15426               NaN  \n",
       "15427               NaN  \n",
       "15428               NaN  \n",
       "15429               NaN  \n",
       "\n",
       "[15430 rows x 6 columns]"
      ]
     },
     "execution_count": 325,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "my"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 326,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "RangeIndex: 15430 entries, 0 to 15429\n",
      "Data columns (total 6 columns):\n",
      " #   Column            Non-Null Count  Dtype  \n",
      "---  ------            --------------  -----  \n",
      " 0   films_type        13584 non-null  object \n",
      " 1   films_area        15430 non-null  object \n",
      " 2   films_duration    15430 non-null  object \n",
      " 3   film_first_time   15427 non-null  object \n",
      " 4   films_score       2520 non-null   float64\n",
      " 5   films_box_office  1501 non-null   float64\n",
      "dtypes: float64(2), object(4)\n",
      "memory usage: 723.4+ KB\n"
     ]
    }
   ],
   "source": [
    "my.info()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 电影类型的缺失值不好填充，删掉电影类型为空的行"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 327,
   "metadata": {},
   "outputs": [],
   "source": [
    "my=my.dropna(subset=['films_type'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 328,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "Int64Index: 13584 entries, 0 to 15429\n",
      "Data columns (total 6 columns):\n",
      " #   Column            Non-Null Count  Dtype  \n",
      "---  ------            --------------  -----  \n",
      " 0   films_type        13584 non-null  object \n",
      " 1   films_area        13584 non-null  object \n",
      " 2   films_duration    13584 non-null  object \n",
      " 3   film_first_time   13581 non-null  object \n",
      " 4   films_score       2510 non-null   float64\n",
      " 5   films_box_office  1501 non-null   float64\n",
      "dtypes: float64(2), object(4)\n",
      "memory usage: 742.9+ KB\n"
     ]
    }
   ],
   "source": [
    "my.info()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 329,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>films_type</th>\n",
       "      <th>films_area</th>\n",
       "      <th>films_duration</th>\n",
       "      <th>film_first_time</th>\n",
       "      <th>films_score</th>\n",
       "      <th>films_box_office</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>喜剧 , 爱情</td>\n",
       "      <td>中国大陆</td>\n",
       "      <td>122分钟</td>\n",
       "      <td>2010-12-22</td>\n",
       "      <td>7.4</td>\n",
       "      <td>47200.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>爱情</td>\n",
       "      <td>中国香港</td>\n",
       "      <td>117分钟</td>\n",
       "      <td>2009-01-26</td>\n",
       "      <td>6.1</td>\n",
       "      <td>11299.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>爱情 , 剧情</td>\n",
       "      <td>中国台湾,中国大陆</td>\n",
       "      <td>128分钟</td>\n",
       "      <td>2012-02-14</td>\n",
       "      <td>7.8</td>\n",
       "      <td>13700.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>爱情</td>\n",
       "      <td>中国大陆,中国香港</td>\n",
       "      <td>112分钟</td>\n",
       "      <td>2012-02-09</td>\n",
       "      <td>7.1</td>\n",
       "      <td>2137.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>爱情 , 音乐 , 奇幻</td>\n",
       "      <td>中国香港,中国台湾</td>\n",
       "      <td>101分钟</td>\n",
       "      <td>2007-07-31</td>\n",
       "      <td>8.5</td>\n",
       "      <td>33.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15423</th>\n",
       "      <td>短片</td>\n",
       "      <td>中国香港</td>\n",
       "      <td>30分钟</td>\n",
       "      <td>2018-05-17</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15426</th>\n",
       "      <td>剧情 , 短片</td>\n",
       "      <td>中国大陆</td>\n",
       "      <td>20分钟</td>\n",
       "      <td>2019-06-25</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15427</th>\n",
       "      <td>纪录片 , 短片</td>\n",
       "      <td>中国大陆</td>\n",
       "      <td>4分钟</td>\n",
       "      <td>2016</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15428</th>\n",
       "      <td>喜剧</td>\n",
       "      <td>中国香港</td>\n",
       "      <td>125分钟</td>\n",
       "      <td>2012-10-11</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15429</th>\n",
       "      <td>纪录片</td>\n",
       "      <td>中国台湾</td>\n",
       "      <td>61分钟</td>\n",
       "      <td>2018-10-25</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>13584 rows × 6 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "           films_type films_area films_duration film_first_time  films_score  \\\n",
       "0            喜剧 , 爱情        中国大陆          122分钟      2010-12-22          7.4   \n",
       "1                 爱情        中国香港          117分钟      2009-01-26          6.1   \n",
       "2            爱情 , 剧情   中国台湾,中国大陆          128分钟      2012-02-14          7.8   \n",
       "3                 爱情   中国大陆,中国香港          112分钟      2012-02-09          7.1   \n",
       "4       爱情 , 音乐 , 奇幻   中国香港,中国台湾          101分钟      2007-07-31          8.5   \n",
       "...               ...        ...            ...             ...          ...   \n",
       "15423             短片        中国香港           30分钟      2018-05-17          NaN   \n",
       "15426        剧情 , 短片        中国大陆           20分钟      2019-06-25          NaN   \n",
       "15427       纪录片 , 短片        中国大陆            4分钟            2016          NaN   \n",
       "15428             喜剧        中国香港          125分钟      2012-10-11          NaN   \n",
       "15429            纪录片        中国台湾           61分钟      2018-10-25          NaN   \n",
       "\n",
       "       films_box_office  \n",
       "0               47200.0  \n",
       "1               11299.0  \n",
       "2               13700.0  \n",
       "3                2137.0  \n",
       "4                  33.0  \n",
       "...                 ...  \n",
       "15423               NaN  \n",
       "15426               NaN  \n",
       "15427               NaN  \n",
       "15428               NaN  \n",
       "15429               NaN  \n",
       "\n",
       "[13584 rows x 6 columns]"
      ]
     },
     "execution_count": 329,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "my"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 对类型、上映地区做处理"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 330,
   "metadata": {},
   "outputs": [],
   "source": [
    "my['films_type'] = my['films_type'].apply(lambda x: x.split(',')[0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 331,
   "metadata": {},
   "outputs": [],
   "source": [
    "def handle_area(x):\n",
    "    area = ['中国大陆','中国香港','中国台湾'] \n",
    "    area_list = x.split(',')\n",
    "    for a in area_list:\n",
    "        if a in area:\n",
    "            return a\n",
    "        else:\n",
    "            continue\n",
    "my['films_area'] = my['films_area'].apply(handle_area)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 332,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "Int64Index: 13584 entries, 0 to 15429\n",
      "Data columns (total 6 columns):\n",
      " #   Column            Non-Null Count  Dtype  \n",
      "---  ------            --------------  -----  \n",
      " 0   films_type        13584 non-null  object \n",
      " 1   films_area        13544 non-null  object \n",
      " 2   films_duration    13584 non-null  object \n",
      " 3   film_first_time   13581 non-null  object \n",
      " 4   films_score       2510 non-null   float64\n",
      " 5   films_box_office  1501 non-null   float64\n",
      "dtypes: float64(2), object(4)\n",
      "memory usage: 742.9+ KB\n"
     ]
    }
   ],
   "source": [
    "my.info()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 333,
   "metadata": {},
   "outputs": [],
   "source": [
    "my=my.dropna(subset=['films_area'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 334,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "Int64Index: 13544 entries, 0 to 15429\n",
      "Data columns (total 6 columns):\n",
      " #   Column            Non-Null Count  Dtype  \n",
      "---  ------            --------------  -----  \n",
      " 0   films_type        13544 non-null  object \n",
      " 1   films_area        13544 non-null  object \n",
      " 2   films_duration    13544 non-null  object \n",
      " 3   film_first_time   13541 non-null  object \n",
      " 4   films_score       2508 non-null   float64\n",
      " 5   films_box_office  1500 non-null   float64\n",
      "dtypes: float64(2), object(4)\n",
      "memory usage: 740.7+ KB\n"
     ]
    }
   ],
   "source": [
    "my.info()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 335,
   "metadata": {},
   "outputs": [],
   "source": [
    "my=my.dropna(subset=['film_first_time'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 336,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "Int64Index: 13541 entries, 0 to 15429\n",
      "Data columns (total 6 columns):\n",
      " #   Column            Non-Null Count  Dtype  \n",
      "---  ------            --------------  -----  \n",
      " 0   films_type        13541 non-null  object \n",
      " 1   films_area        13541 non-null  object \n",
      " 2   films_duration    13541 non-null  object \n",
      " 3   film_first_time   13541 non-null  object \n",
      " 4   films_score       2506 non-null   float64\n",
      " 5   films_box_office  1500 non-null   float64\n",
      "dtypes: float64(2), object(4)\n",
      "memory usage: 740.5+ KB\n"
     ]
    }
   ],
   "source": [
    "my.info()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 其他特征的处理"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 337,
   "metadata": {},
   "outputs": [],
   "source": [
    "my['films_duration'] = my['films_duration'].apply(lambda x: int(x[:-2]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 338,
   "metadata": {},
   "outputs": [],
   "source": [
    "my['film_first_time'] = my['film_first_time'].apply(lambda x: int(x[0:4]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 339,
   "metadata": {},
   "outputs": [],
   "source": [
    "my['films_score'] = my['films_score'].apply(lambda x: float(x))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 340,
   "metadata": {},
   "outputs": [],
   "source": [
    "my['films_box_office'] = my['films_box_office'].apply(lambda x: float(x))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 341,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "Int64Index: 13541 entries, 0 to 15429\n",
      "Data columns (total 6 columns):\n",
      " #   Column            Non-Null Count  Dtype  \n",
      "---  ------            --------------  -----  \n",
      " 0   films_type        13541 non-null  object \n",
      " 1   films_area        13541 non-null  object \n",
      " 2   films_duration    13541 non-null  int64  \n",
      " 3   film_first_time   13541 non-null  int64  \n",
      " 4   films_score       2506 non-null   float64\n",
      " 5   films_box_office  1500 non-null   float64\n",
      "dtypes: float64(2), int64(2), object(2)\n",
      "memory usage: 740.5+ KB\n"
     ]
    }
   ],
   "source": [
    "my.info()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 提取特征 独热编码"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 342,
   "metadata": {},
   "outputs": [],
   "source": [
    "f_type = pd.get_dummies(my['films_type']) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 343,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>Sci-Fi</th>\n",
       "      <th>亲情</th>\n",
       "      <th>传记</th>\n",
       "      <th>儿童</th>\n",
       "      <th>公益</th>\n",
       "      <th>公益性</th>\n",
       "      <th>公路喜剧</th>\n",
       "      <th>冒险</th>\n",
       "      <th>军旅</th>\n",
       "      <th>...</th>\n",
       "      <th>警匪</th>\n",
       "      <th>记录片</th>\n",
       "      <th>运动</th>\n",
       "      <th>都市</th>\n",
       "      <th>青春</th>\n",
       "      <th>青春 励志 爱情</th>\n",
       "      <th>青春励志</th>\n",
       "      <th>音乐</th>\n",
       "      <th>魔幻</th>\n",
       "      <th>黑色电影</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>3 rows × 79 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "        Sci-Fi    亲情    传记    儿童    公益    公益性    公路喜剧    冒险    军旅   ...   警匪   \\\n",
       "0   0         0     0     0     0     0      0       0     0     0  ...     0   \n",
       "1   0         0     0     0     0     0      0       0     0     0  ...     0   \n",
       "2   0         0     0     0     0     0      0       0     0     0  ...     0   \n",
       "\n",
       "    记录片    运动    都市    青春    青春 励志 爱情    青春励志    音乐    魔幻    黑色电影   \n",
       "0      0     0     0     0           0       0     0     0       0  \n",
       "1      0     0     0     0           0       0     0     0       0  \n",
       "2      0     0     0     0           0       0     0     0       0  \n",
       "\n",
       "[3 rows x 79 columns]"
      ]
     },
     "execution_count": 343,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "f_type.head(3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 344,
   "metadata": {},
   "outputs": [],
   "source": [
    "f_area = pd.get_dummies(my['films_area'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 345,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>中国台湾</th>\n",
       "      <th>中国大陆</th>\n",
       "      <th>中国香港</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   中国台湾  中国大陆  中国香港\n",
       "0     0     1     0\n",
       "1     0     0     1\n",
       "2     1     0     0"
      ]
     },
     "execution_count": 345,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "f_area.head(3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 346,
   "metadata": {},
   "outputs": [],
   "source": [
    "my = my.join(f_type)\n",
    "my = my.join(f_area)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 347,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>films_type</th>\n",
       "      <th>films_area</th>\n",
       "      <th>films_duration</th>\n",
       "      <th>film_first_time</th>\n",
       "      <th>films_score</th>\n",
       "      <th>films_box_office</th>\n",
       "      <th></th>\n",
       "      <th>Sci-Fi</th>\n",
       "      <th>亲情</th>\n",
       "      <th>传记</th>\n",
       "      <th>...</th>\n",
       "      <th>都市</th>\n",
       "      <th>青春</th>\n",
       "      <th>青春 励志 爱情</th>\n",
       "      <th>青春励志</th>\n",
       "      <th>音乐</th>\n",
       "      <th>魔幻</th>\n",
       "      <th>黑色电影</th>\n",
       "      <th>中国台湾</th>\n",
       "      <th>中国大陆</th>\n",
       "      <th>中国香港</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>喜剧</td>\n",
       "      <td>中国大陆</td>\n",
       "      <td>122</td>\n",
       "      <td>2010</td>\n",
       "      <td>7.4</td>\n",
       "      <td>47200.0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>爱情</td>\n",
       "      <td>中国香港</td>\n",
       "      <td>117</td>\n",
       "      <td>2009</td>\n",
       "      <td>6.1</td>\n",
       "      <td>11299.0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>爱情</td>\n",
       "      <td>中国台湾</td>\n",
       "      <td>128</td>\n",
       "      <td>2012</td>\n",
       "      <td>7.8</td>\n",
       "      <td>13700.0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>爱情</td>\n",
       "      <td>中国大陆</td>\n",
       "      <td>112</td>\n",
       "      <td>2012</td>\n",
       "      <td>7.1</td>\n",
       "      <td>2137.0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>爱情</td>\n",
       "      <td>中国香港</td>\n",
       "      <td>101</td>\n",
       "      <td>2007</td>\n",
       "      <td>8.5</td>\n",
       "      <td>33.0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 88 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "  films_type films_area  films_duration  film_first_time  films_score  \\\n",
       "0        喜剧        中国大陆             122             2010          7.4   \n",
       "1        爱情        中国香港             117             2009          6.1   \n",
       "2        爱情        中国台湾             128             2012          7.8   \n",
       "3        爱情        中国大陆             112             2012          7.1   \n",
       "4        爱情        中国香港             101             2007          8.5   \n",
       "\n",
       "   films_box_office       Sci-Fi    亲情    传记   ...   都市    青春    青春 励志 爱情   \\\n",
       "0           47200.0   0         0     0     0  ...     0     0           0   \n",
       "1           11299.0   0         0     0     0  ...     0     0           0   \n",
       "2           13700.0   0         0     0     0  ...     0     0           0   \n",
       "3            2137.0   0         0     0     0  ...     0     0           0   \n",
       "4              33.0   0         0     0     0  ...     0     0           0   \n",
       "\n",
       "    青春励志    音乐    魔幻    黑色电影   中国台湾  中国大陆  中国香港  \n",
       "0       0     0     0       0     0     1     0  \n",
       "1       0     0     0       0     0     0     1  \n",
       "2       0     0     0       0     1     0     0  \n",
       "3       0     0     0       0     0     1     0  \n",
       "4       0     0     0       0     0     0     1  \n",
       "\n",
       "[5 rows x 88 columns]"
      ]
     },
     "execution_count": 347,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "my.head(5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 348,
   "metadata": {},
   "outputs": [],
   "source": [
    "my = my.drop(['films_type','films_area'],axis=1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 缺失值填充 中位数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 349,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 中位数\n",
    "f_score_median = my['films_score'].median()\n",
    "f_office_median = my['films_box_office'].median()\n",
    "my['films_score'] =my['films_score'].fillna(f_score_median)\n",
    "my['films_box_office'] = my['films_box_office'].fillna(f_office_median)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 特征选择"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 362,
   "metadata": {},
   "outputs": [],
   "source": [
    "data = my.loc[:,my.columns != 'films_box_office'].values\n",
    "label = my.loc[:,my.columns == 'films_box_office'].values"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 363,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 122. , 2010. ,    7.4, ...,    0. ,    1. ,    0. ],\n",
       "       [ 117. , 2009. ,    6.1, ...,    0. ,    0. ,    1. ],\n",
       "       [ 128. , 2012. ,    7.8, ...,    1. ,    0. ,    0. ],\n",
       "       ...,\n",
       "       [   4. , 2016. ,    6.9, ...,    0. ,    1. ,    0. ],\n",
       "       [ 125. , 2012. ,    6.9, ...,    0. ,    0. ,    1. ],\n",
       "       [  61. , 2018. ,    6.9, ...,    1. ,    0. ,    0. ]])"
      ]
     },
     "execution_count": 363,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.set_printoptions(suppress=True)\n",
    "data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 366,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([47200. , 11299. , 13700. , ...,   315.5,   315.5,   315.5])"
      ]
     },
     "execution_count": 366,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "label = label.flatten()\n",
    "label"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.feature_selection import RFE\n",
    "from sklearn.linear_model import LogisticRegression\n",
    "\n",
    "#递归特征消除法，返回特征选择后的数据\n",
    "#参数estimator为基模型\n",
    "#参数n_features_to_select为选择的特征个数\n",
    "featureArr = RFE(estimator=LogisticRegression(), n_features_to_select=12).fit_transform(data, label.astype('int'))\n",
    "print(featureArr)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
